package ar.com.ford.it.fullEconomics.persistence.dao;

import java.sql.Timestamp;
import java.util.Collection;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp04Supply;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp05Pricing;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class PricingDao extends Dao {
    private static final String CLASS_NAME = PricingDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);
    public Collection<Teesp05Pricing> findPricing(Teesp05Pricing pricing,Timestamp fechaInStart,Timestamp fechaInEnd,Timestamp fechaOutStart,Timestamp fechaOutEnd)
    throws PersistenceException {

        String METHOD_NAME = "findSupply";
        log.entering(CLASS_NAME, METHOD_NAME);
        
        Collection<Teesp05Pricing> supplyCol = null;
        PersistenceManager pm = null;
        Object[] param = new Object[5];
        param[0] = pricing;
        param[1] = fechaInStart;
        param[2] = fechaInEnd;
        param[3] = fechaOutStart;
        param[4] = fechaOutEnd;
        
        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findPricingByFilters", Teesp05Pricing.class);
        query.setParameterArray(param);
        
        supplyCol = (Collection) pm.executeQuery(query);
        
        log.exiting(CLASS_NAME, METHOD_NAME);
        return supplyCol;
    }
    public PricingDao() {
        super();
    }

    /**
     * Create Pricing
     * 
     * @param newPricing
     * @throws PersistenceException
     */
    public void createPricing(Teesp05Pricing newPricing)
            throws PersistenceException {
        String METHOD_NAME = "createPricing";
        log.entering(CLASS_NAME, METHOD_NAME);
        Transaction tx = null;
        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(newPricing);
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Elimina de forma definitiva de la base de datos el Pricing pasado por
     * parametro
     * 
     * @param Pricing
     * Debe haber sido obtenido de la base de datos
     * @throws PersistenceException
     * @throws PersistenceException
     */
    public void deletePricing(Teesp05Pricing pricing) throws PersistenceException {
        String METHOD_NAME = "deletePricing";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(pricing);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }
    
    /**
     * Retorna objeto para actualizacion
     * @param pricing
     * @return
     * @throws PersistenceException
     */
     public Teesp05Pricing getUpdatePricing(Teesp05Pricing pricing) throws PersistenceException {

            String METHOD_NAME = "getUpdatePricing";
            log.entering(CLASS_NAME, METHOD_NAME);

            Teesp05Pricing result = null;
            result = (Teesp05Pricing) super.getObjForUpdate(pricing);

            log.exiting(CLASS_NAME, METHOD_NAME);
            return result;
      }
     
     public Collection<Teesp05Pricing> overloadDateEffective(Timestamp fechaInStart,Timestamp fechaInEnd,Timestamp fechaOutStart,Timestamp fechaOutEnd)
     throws PersistenceException {

         String METHOD_NAME = "findSupply";
         log.entering(CLASS_NAME, METHOD_NAME);
         
         Collection<Teesp05Pricing> prcingCol = null;
         PersistenceManager pm = null;
         Object[] param = new Object[4];
         param[0] = fechaInStart;
         param[1] = fechaInEnd;
         param[2] = fechaOutStart;
         param[3] = fechaOutEnd;
         
         pm = PersistenceUtil.getPersistenceManager();
         Query query = pm.newQuery("findOverloadDateEffective", Teesp05Pricing.class);
         query.setParameterArray(param);
         
         prcingCol = (Collection) pm.executeQuery(query);
         
         log.exiting(CLASS_NAME, METHOD_NAME);
         return prcingCol;
     }
     /**
      * Devuelve una coleccion de pricings buscando por proveedor, pieza, y 
      * @param pricing
      * @param fechaInS
      * @param fechaInE
      * @return
      * @throws PersistenceException
      */
    public Collection<Teesp05Pricing> findPricingExisting(Teesp05Pricing pricing, Timestamp fechaInS, Timestamp fechaInE, Integer sortInOut) throws PersistenceException {
        
        String METHOD_NAME = "findPricingExisting";
        log.entering(CLASS_NAME, METHOD_NAME);
        Collection<Teesp05Pricing> pricR = null;
        PersistenceManager pm = null;
        Object[] param = new Object[4];
        param[0] = pricing;
        param[1] = fechaInS;
        param[2] = fechaInE;
        param[3] = sortInOut;
        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findPricingExisting", Teesp05Pricing.class);
        query.setParameterArray(param);
        
        pricR = (Collection) pm.executeQuery(query);
        
        log.exiting(CLASS_NAME, METHOD_NAME);
        return pricR;
    }
    
    /**
     * Busca el Price del Supply pasado por parametro para la fecha "date"
     * 
     * @param supply
     * @param date
     * @return
     * @throws PersistenceException 
     */
    public Teesp05Pricing findPricingByDate(Teesp04Supply supply, Timestamp date)
            throws PersistenceException {
        String METHOD_NAME = "findPricingByDate";
        log.entering(CLASS_NAME, METHOD_NAME);
        
        Teesp05Pricing pricing = null;
        PersistenceManager pm = null;
        
        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findPricingByDate", Teesp05Pricing.class);
        query.setParameters(supply, date);
        
        pricing = (Teesp05Pricing) pm.executeQuery(query);
        
        log.exiting(CLASS_NAME, METHOD_NAME);
        return pricing;
    }
     
}
